package com.edushare.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

public class UserFunctions {

	private class Login extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			JSONObject json = jsonParser.getJSONFromUrl(loginURL, params[0]);
			return json;
		}
	}

	private class Register extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			// getting JSON Object
			JSONObject json = jsonParser.getJSONFromUrl(registerURL, params[0]);
			return json;
		}
	}

	private JSONParser jsonParser;
	private static String loginURL = "http://unifiq.5kportal.com/login/";
	private static String registerURL = "http://unifiq.5kportal.com/login/";
	private static String login_tag = "login";
	private static String register_tag = "register";

	// constructor
	public UserFunctions() {
		jsonParser = new JSONParser();
	}

	public String getUid(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		String uid = db.getUid();
		return uid;
	}

	public HashMap<String, String> getUser(Context context) {
		if (isUserLoggedIn(context)) {
			DatabaseHandler db = new DatabaseHandler(context);
			return db.getUserDetails();
		}
		return null;
	}

	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCount();
		if (count > 0) {
			// user logged in
			return true;
		}
		return false;
	}

	/**
	 * function make Login Request
	 * 
	 * @param email
	 * @param password
	 * */
	public JSONObject loginUser(String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", login_tag));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		// Log.e("JSON", json.toString());
		JSONObject jso = new JSONObject();
		try {
			jso = new Login().execute(params).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jso;
	}

	/**
	 * Function to logout user Reset Database
	 * */
	public boolean logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}

	/**
	 * function make Login Request
	 * 
	 * @param name
	 * @param email
	 * @param password
	 * */
	public JSONObject registerUser(String name, String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", register_tag));
		params.add(new BasicNameValuePair("name", name));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));

		JSONObject jso = new JSONObject();
		try {
			jso = new Register().execute(params).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jso;
	}

}
